package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Order;
import domain.OrderList;
import factory.ServiceFactory;
import net.sf.json.JSONObject;
import util.FloatUtil;

/**
 * Servlet implementation class Chart
 */
@WebServlet("/charts.do")
public class Chart extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Chart() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String meth = request.getParameter("method");
		if (meth == null || meth.equals("")) {
			meth = "index";
		}
		switch (meth) {
		case "index":
			request.getRequestDispatcher("charts.html").forward(request, response);
			break;
		case "sale":
			getSale(request, response);
			break;
		case "order":
			getOrderInfo(request, response);
			break;
		default:
			break;
		}
	}

	protected void getOrderInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		String pagestring = request.getParameter("page");
		int page = 1;
		if (!(pagestring == null || pagestring.equals("")))
			page = Integer.valueOf(pagestring);
		long count = ServiceFactory.getSerInstance().getOrdersCount();
		int all = (int) count / 20;
		if (count % 20 > 0) {
			all++;
		}
		if (all == 0)
			all = 1;
		if (page < 0 || (page - 1) * 20 > count) {
			JSONObject json = new JSONObject();
			json.put("status", 500);
			json.put("count", all);
			json.put("msg", "页数超出范围！");
			out.print(json);
		} else {
			List<Order> orders = ServiceFactory.getSerInstance().getTwntyOrders(page);
			String json = "{\"status\":200,\"count\":\"" + all + "\",\"content\":[";
			for (int i = 0, l = orders.size(); i < l; i++) {
				if (i == 0) {
					json += "{" + "\"ss\":\"" + orders.get(i).getSs();
					if(orders.get(i).getPay() == 0)
						json += "(现金)";
					json += "\"," + "\"yj\":\"" + orders.get(i).getYj()
							+ "\"," + "\"rq\":\"" + orders.get(i).getDate().toString() + "\"," + "\"list\":[";
					List<OrderList> lists = ServiceFactory.getSerInstance().getOneOrderList(orders.get(i).getId());
					for (int j = 0, k = lists.size(); j < k; j++) {
						domain.Goods goods = ServiceFactory.getSerInstance().getOneGoodsInfo(lists.get(j).getTxm());
						if (j == 0) {
							json += "{\"name\":\"" + goods.getName() + "\"," + "\"sl\":\"" + lists.get(j).getSl()
									+ "\"," + "\"zj\":\"" + FloatUtil.floatSaveTwoD(lists.get(j).getZj()) + "\"";
							json += "}";
						} else {
							json += ",{\"name\":\"" + goods.getName() + "\"," + "\"sl\":\"" + lists.get(j).getSl()
									+ "\"," + "\"zj\":\"" + FloatUtil.floatSaveTwoD(lists.get(j).getZj()) + "\"";
							json += "}";
						}
					}
					json += "]}";
				} else {
					json += ",{" + "\"ss\":\"" + orders.get(i).getSs();
					if(orders.get(i).getPay() == 0)
						json += "(现金)";
					json += "\"," + "\"yj\":\"" + orders.get(i).getYj()
							+ "\"," + "\"rq\":\"" + orders.get(i).getDate().toString() + "\"," + "\"list\":[";
					List<OrderList> lists = ServiceFactory.getSerInstance().getOneOrderList(orders.get(i).getId());
					for (int j = 0, k = lists.size(); j < k; j++) {
						domain.Goods goods = ServiceFactory.getSerInstance().getOneGoodsInfo(lists.get(j).getTxm());
						if (j == 0) {
							json += "{\"name\":\"" + goods.getName() + "\"," + "\"sl\":\"" + lists.get(j).getSl()
									+ "\"," + "\"zj\":\"" + FloatUtil.floatSaveTwoD(lists.get(j).getZj()) + "\"";
							json += "}";
						} else {
							json += ",{\"name\":\"" + goods.getName() + "\"," + "\"sl\":\"" + lists.get(j).getSl()
									+ "\"," + "\"zj\":\"" + FloatUtil.floatSaveTwoD(lists.get(j).getZj()) + "\"";
							json += "}";
						}
					}
					json += "]}";
				}
			}
			json += "]}";
			out.println(json);
		}
		out.close();
	}

	protected void getSale(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		JSONObject json = new JSONObject();
		PrintWriter out = response.getWriter();
		String year = request.getParameter("year");
		String month = request.getParameter("month");
		if (year == null || year.equals("") || month == null || month.equals("")) {
			json.put("status", 500);
			out.print(json);
			out.close();
			return;
		}
		String dateString1 = year + "-" + month + "-01";
		String dateString2 = year + "-" + month + "-31";
		List<Order> orders = ServiceFactory.getSerInstance().getOneMonthOrders(dateString1, dateString2);
		float monthSale = 0;
		float cashSale = 0;
		int sales = 0;
		if (orders != null) {
			sales = orders.size();
			for (Order order : orders) {
				monthSale += order.getSs();
				if (order.getPay() == 0) {
					cashSale += order.getSs();
				}
			}
		}
		json.put("status", 200);
		json.put("monthSale", String.valueOf(FloatUtil.floatSaveTwoD(monthSale)));
		json.put("cashSale", String.valueOf(FloatUtil.floatSaveTwoD(cashSale)));
		json.put("sales", String.valueOf(sales));
		out.print(json);
		out.close();
		return;
	}

}
